﻿using System;

namespace CustomTypeConversion
{
    public class Currency
    {
        public decimal Amount { get; set; }
        public Currency(decimal amount)
        {
            Amount = amount;
        }
        // 显式类型转换操作符
        public static explicit operator float(Currency currency)
        {
            return (float)currency.Amount;
        }
    }


    public class FahrenheitTemperature
    {
        public double Fahrenheit { get; set; }

        public FahrenheitTemperature(double fahrenheit)
        {
            Fahrenheit = fahrenheit;
        }

        // 定义显式类型转换操作符，将Temperature对象转换为Celsius对象
        public static explicit operator CelsiusTemperature(FahrenheitTemperature temp)
        {
            return new CelsiusTemperature((temp.Fahrenheit - 32) * 5 / 9);
        }
    }

    public class CelsiusTemperature
    {
        public double Degrees { get; set; }

        public CelsiusTemperature(double degrees)
        {
            Degrees = degrees;
        }

        // 定义隐式类型转换操作符，将Celsius对象转换为Temperature对象
        public static implicit operator FahrenheitTemperature(CelsiusTemperature celsius)
        {
            return new FahrenheitTemperature(celsius.Degrees * 9 / 5 + 32);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            FahrenheitTemperature temp = new FahrenheitTemperature(68);
            CelsiusTemperature celsius = (CelsiusTemperature)temp; // 显式类型转换
            FahrenheitTemperature temp2 = celsius; // 隐式类型转换
        }
    }
}
